package com.library_manage_system.strategy;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.library_manage_system.view.WindowsMainImpl;
import com.library_manager_system.Book;
import com.library_manager_system.utils.GetBookByName;

public class ModifyBookPanelStrategy implements PanelStrategy {

	private JButton jButton;
	private JLabel jl1, jl2, jl3, jl4, jl5, jl6, jl7, jl8, jl9, jl10;
	private JTextField jt1, jt2, jt3, jt4, jt5, jt6, jt7, jt8, jt9, jt10;
	private JPanel bookDatamodifyPanel;
	private JPanel loggingData;
	private JButton summit;
	private JComboBox jComboBox;
	private String value;
	
	
	@Override
	public JPanel createPanel(final ArrayList<Book> arraylist,final WindowsMainImpl windowsMainImpl) {
		// TODO Auto-generated method stub
	
		final JPanel modifyBookPanel = new JPanel();
		final CardLayout cardLayout = new CardLayout();
		modifyBookPanel.setLayout(cardLayout);
		// 设置一个label来提示客户信息，提问想要还哪一本书
		JLabel jLabel = new JLabel("请问你想要修改哪一本书");

		// 设置一个下拉框用来让客户选择想要修改哪一本书
		jComboBox = null;
		int size = arraylist.size();

		if (size == 0) {

			String[] s = { "无" };
			jComboBox = new JComboBox(s);

		} else {
			String[] s = new String[size];
			Iterator<Book> it = arraylist.iterator();
			while (it.hasNext()) {
				s[size - 1] = it.next().getBook_name();
				size--;
				jComboBox = new JComboBox(s);
			}
		}

		// 设置一个按钮表示提交想要借的书
		jButton = new JButton("确定");
		summit = new JButton("确定");
		// jButton.setPreferredSize(new Dimension(50,30));
		// jButton.setLocation(50, 50);

		JPanel jPanel = new JPanel();
		jPanel.setLayout(new GridLayout(3, 1, 50, 50));
		jPanel.add(jLabel);
		jPanel.add(jComboBox);
		jPanel.add(jButton);
		JPanel jp = new JPanel();
		jp.add(jPanel);

		// 添加一个修改面板
		CreateModifyData(arraylist);

		// 新建一个卡片面板
		modifyBookPanel.add(jp, "mainPanel");
		cardLayout.show(modifyBookPanel, "mainPanel");
		jButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// 更新修改面板
				CreateModifyData(arraylist);
				modifyBookPanel.add(bookDatamodifyPanel, "bookDatamodifyPanel");
				cardLayout.show(modifyBookPanel, "bookDatamodifyPanel");
			}
		});

		// 为按钮summit添加事件
		summit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// 当用户修改完按确定后，要对数据进行重新保存
				String value2 = (String) jComboBox.getSelectedItem();

				Book book = GetBookByName.getBookByName(arraylist, value2);
				book.setAuthor(jt1.getText().trim());
				book.setPress(jt2.getText().trim());
				book.setPress_year(jt3.getText().trim());
				book.setBook_name(jt6.getText().trim());

				book.setPress_time(Integer.parseInt(jt4.getText().trim()));
				book.setISBN(Integer.parseInt(jt5.getText().trim()));
				book.setBook_price(Integer.parseInt(jt7.getText().trim()));
				book.setBook_number(Integer.parseInt(jt8.getText().trim()));
				book.setBorrow_book_number(Integer.parseInt(jt9.getText()
						.trim()));
				int the_rest_number = (Integer.parseInt(jt8.getText().trim()))
						- (Integer.parseInt(jt9.getText().trim()));
				book.setThe_rest_number(the_rest_number);

				WindowsMainImpl.init(arraylist,windowsMainImpl);
				cardLayout.show(modifyBookPanel, "mainPanel");
			}
		});

		return modifyBookPanel;
	}

	// 创建一个修改面板
	public void CreateModifyData(ArrayList<Book> arraylist) {

		// 给jlabel进行初始化
		jl1 = new JLabel("主编");
		jl2 = new JLabel("出版社");
		jl3 = new JLabel("出版年");
		jl4 = new JLabel("版次");
		jl5 = new JLabel("ISBN");
		jl6 = new JLabel("书名");
		jl7 = new JLabel("定价");
		jl8 = new JLabel("拥有本数");
		jl9 = new JLabel("借出本数");
		jl10 = new JLabel("现余本数");

		// 给文本框进行初始化
		jt1 = new JTextField(10);
		jt2 = new JTextField(10);
		jt3 = new JTextField(10);
		jt4 = new JTextField(10);
		jt5 = new JTextField(10);
		jt6 = new JTextField(10);
		jt7 = new JTextField(10);
		jt8 = new JTextField(10);
		jt9 = new JTextField(10);
		jt10 = new JTextField(10);

		// 对jt4,jt5,jt7,jt8,jt9,jt10添加监听，使他们只能输入数字
		jt4.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt5.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt7.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt8.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt9.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt9.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				if ((!(jt8.getText().equals("")))
						&& (!(jt9.getText().equals("")))) {
					if ((Integer.parseInt(jt9.getText().trim())) >= (Integer
							.parseInt(jt8.getText().trim()))) {
						JOptionPane.showMessageDialog(null, "你输入的值必须小于拥有本数",
								"图书馆管理系统", JOptionPane.ERROR_MESSAGE);
					}
				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub

			}
		});
		jt10.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				if ((!(jt8.getText().equals("")))
						&& (!(jt9.getText().equals("")))) {
					int t = (Integer.parseInt(jt8.getText()))
							- (Integer.parseInt(jt9.getText()));
					String t1 = Integer.toString(t);
					if (t <= 0) {

					} else {
						jt10.setText(t1);
					}
				}
			}
		});

		// 获取用户选的下拉框的值，同时判断是否需要新建这个面板

		if (jComboBox.getSelectedItem().equals("无")) {
			return;
		} else {
			value = (String) jComboBox.getSelectedItem();

			if (!(value.equals(""))) {
				// 给书籍的信息进行初始化

				init(value, arraylist);
			}

		}

		bookDatamodifyPanel = new JPanel();
		bookDatamodifyPanel.setLayout(new BorderLayout());

		loggingData = new JPanel();
		// 给这个面板设置网格布局，用于填写书籍的信息并录入
		loggingData.setLayout(new GridLayout(5, 4, 5, 5));

		// 添加到logginPanel中
		loggingData.add(jl1);
		loggingData.add(jt1);

		loggingData.add(jl2);
		loggingData.add(jt2);

		loggingData.add(jl3);
		loggingData.add(jt3);

		loggingData.add(jl4);
		loggingData.add(jt4);

		loggingData.add(jl5);
		loggingData.add(jt5);

		loggingData.add(jl6);
		loggingData.add(jt6);

		loggingData.add(jl7);
		loggingData.add(jt7);

		loggingData.add(jl8);
		loggingData.add(jt8);

		loggingData.add(jl9);
		loggingData.add(jt9);

		loggingData.add(jl10);
		loggingData.add(jt10);

		JLabel jlabel = new JLabel("请在下面填写书籍的修改信息");
		JPanel jpanel3 = new JPanel();
		jpanel3.add(jlabel);

		JPanel jpanel2 = new JPanel();
		jpanel2.add(loggingData);
		bookDatamodifyPanel.add(jpanel3, BorderLayout.NORTH);
		bookDatamodifyPanel.add(jpanel2, BorderLayout.CENTER);

		JPanel login = new JPanel();
		login.add(summit);

		bookDatamodifyPanel.add(login, BorderLayout.SOUTH);

	}

	// 给书籍的信息进行初始化
	public void init(String value, ArrayList<Book> arraylist) {

		// 获取用户选的书对象
		Book book = GetBookByName.getBookByName(arraylist, value);

		jt1.setText(book.getAuthor());
		jt2.setText(book.getPress());
		jt3.setText(book.getPress_year());
		jt4.setText(String.valueOf(book.getPress_time()));
		jt5.setText(String.valueOf(book.getISBN()));

		jt6.setText(String.valueOf(book.getBook_name()));
		jt7.setText(String.valueOf(book.getBook_price()));
		jt8.setText(String.valueOf(book.getBook_number()));
		jt9.setText(String.valueOf(book.getBorrow_book_number()));
		jt10.setText(String.valueOf(book.getThe_rest_number()));
	}

}
